#include "NxDataFactory.h"
#include "NxData.h"
#include "NxDataConfiguration.h"
#include "NxDataFactory.h"

NJGIS::SERVICE::INjData* NJGIS::SERVICE::NjDataFactory::createDataByJSON( Json::Value &jData, const char* ip, int port )
{
	std::string gd_id = jData["gd_id"].asString();
	std::string gd_tag = jData["gd_tag"].asString();
	std::string gd_datetime = jData["gd_datetime"].asString();
	std::string gd_type = jData["gd_type"].asString();
	NJGIS::SERVICE::NjDataType type = NJGIS::SERVICE::NjDataFactory::convertString2DataType(gd_type.c_str());
	std::string sszie = jData["gd_size"].asString();
	int gd_size = atoi(sszie.c_str());
	std::string gd_value = jData["gd_value"].asString();
	NJGIS::SERVICE::INjData* pData = new NJGIS::SERVICE::NjData(gd_id.c_str(), gd_tag.c_str(), gd_size, gd_datetime.c_str(), type, gd_value.c_str(), ip, port);
	return pData;
}

NJGIS::SERVICE::INjDataConfiguration* NJGIS::SERVICE::NjDataFactory::createDataConfigByJSON( Json::Value &jConfig )
{
	INjDataConfiguration* pDataConfig = new NJGIS::SERVICE::NjDataConfiguration();
	for(int i = 0; i < jConfig.size(); i++)
	{
		std::string stateid = jConfig[i]["StateId"].asString();
		std::string eventname = jConfig[i]["Event"].asString();
		std::string dataid = jConfig[i]["DataId"].asString();
		bool destoryed = jConfig[i]["Destroyed"].asBool();
		pDataConfig->insertData(stateid.c_str(), eventname.c_str(), dataid.c_str());
	}
	return pDataConfig;
}

NJGIS::SERVICE::NjDataType NJGIS::SERVICE::NjDataFactory::convertString2DataType( const char* ctype )
{
	NJGIS::SERVICE::NjDataType type;
	std::string stype(ctype);
	if (stype == "XML")
	{
		type = NJGIS::SERVICE::NjDataType::DAT_XML;
	}
	else if (stype == "ZIP")
	{
		type = NJGIS::SERVICE::NjDataType::DAT_ZIP;
	}
	else if (stype == "RAW")
	{
		type = NJGIS::SERVICE::NjDataType::DAT_RAW;
	}
	else
	{
		type = NJGIS::SERVICE::NjDataType::DAT_UNKNOWN;
	}
	return type;
}
